package com.seaboxdata.interfaceJob.yxzsxt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.yxzsxt.FlowDetails;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import com.seaboxdata.utils.TransRealHttpsHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @ClassName FlowDetailsJob
 * @Deacription TODO 获取某一交叉口各个方向的通道流量
 * @Author lh
 * @Date 2021/9/26
 * @Version 1.0
 **/
@Component
@Configuration
@Slf4j
public class FlowDetailsJob {

    @Value("${real.trans.url.FlowDetails}")
    private String url;

    @Value("${real.trans.token.FlowDetails}")
    private String token;

    @Value("${real.trans.FlowDetailsTopic}")
    private String topic;

    @Value("${interface.isTest}")
    private String isTest;

    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;

    private static final int retry = 3;

    private static int countRetry = 0;

    @Scheduled(cron = "${real.trans.flowDetailsJob}")//每隔5分钟执行一次
//    @Scheduled(cron="0/1 * * * * ?")   //每隔一秒执行
    public void doCollect() {
        //如果是测试,直接退出
        if("true".equals(isTest)){
            return;
        }
        log.info("调用 获取某一交叉口各个方向的通道流量信息 start");
        try {
            List<FlowDetails> flowDetailsJob = retryGetData(url, token);
            if (!CollectionUtils.isEmpty(flowDetailsJob)) {
                //写入kafka
                Date date = new Date();
                for (FlowDetails flowDetails : flowDetailsJob) {
                    flowDetails.setCjsj(date);
                    baseStringKafkaProducer.sendMessageToKafka(JSONObject.toJSONString(flowDetails), topic);
                }
            }
        } catch (Exception e) {
            log.error("调用 获取某一交叉口各个方向的通道流量信息 异常");
            e.printStackTrace();
        }
        log.info("调用 获取某一交叉口各个方向的通道流量信息 end");
    }

    public List<FlowDetails> retryGetData(String url, String token) {

        List<FlowDetails> flowDetailsJob = null;
        try {
            flowDetailsJob = TransRealHttpsHandler.sendGet(url, token, FlowDetails.class);
            log.info("flowDetailsJob======{}",flowDetailsJob.size());
            if (!(CollectionUtils.isNotEmpty(flowDetailsJob) && flowDetailsJob.get(0) != null)) {
                if (countRetry == retry) {
                    log.error("we have try {} to getData,but still have error", countRetry);
                    return null;
                } else {
                    countRetry++;
                    Thread.sleep(1000);
                    flowDetailsJob = retryGetData(url, token);
                }
            } else {
                countRetry = 0;
            }
        } catch (Exception e) {
            log.error("调用 获取某一交叉口各个方向的通道流量信息 异常");
            e.printStackTrace();
        }
        return flowDetailsJob;
    }

}
